#include "OSS_Upload.h"
#include "tests/cefclient/browser/protocol.h"
#include <conio.h> 
 

static const char * OSS_TYPE_MP3 = "audio/mp3";
static const char * OSS_TYPE_JPEG = "image/jpeg";
static const char * OSS_TYPE_MP4 = "video/mpeg4";   //.mp4
static const char * OSS_TYPE_AVI = "video/avi";     //.avi
static const char * OSS_TYPE_UNKNOWN = "application/octet-stream";     //.avi

static const char * OSS_TYPE_TXT = "text/plain";
static const char * OSS_TYPE_BINARY = "application/octet-stream";

static const char OSS_ENDPOINT[] = "oss-cn-shanghai.aliyuncs.com";
static const char ACCESS_KEY_ID[] = "LTAIPapGv0jON3YL";
static const char ACCESS_KEY_SECRET[] = "j4k5xIKFwMxm9Nlt9pi17vXWj3HyUv";
static const char BUCKET_NAME[] = "upupoo-front-test";
static const char TEST_OBJDIR[] = "test/";

OSS_Upload::OSS_Upload(std::string  localpaperpath, std::string  paperid, int themetype, std::string  src)
{
	this->local_paper_path_ = localpaperpath; 
	this->themetype_ = themetype;
	this->src_ = src;
	this->paperid_ = paperid;
}

OSS_Upload::~OSS_Upload()
{ 
	if (LOGDEBUG) _cprintf(":~OSS_Upload->\n");
	//aos_http_io_deinitialize();
	//if (LOGDEBUG) _cprintf(":~OSS_Upload<-\n");
}

void OSS_Upload::init() {
	// initialize http io system, call it olny once
	if (aos_http_io_initialize(NULL, 0) != AOSE_OK) {
		if (LOGDEBUG) _cprintf("oss initialize failed\n");
		return;
	}
	if (LOGDEBUG) _cprintf("aos initalize done\n");
}

void OSS_Upload::release() {
	aos_http_io_deinitialize();
}

void OSS_Upload::UploadFile() {
	if (!UploadPreview()) return;

	if (!UploadUpup())  return;

	UploadData();
}

bool OSS_Upload::UploadPreview()
{ 
	aos_pool_create(&pool_, NULL);

	aos_pool_t *p = NULL;
	aos_string_t bucket;
	aos_string_t object;
	int is_cname = 0;
	aos_table_t *headers = NULL;
	aos_table_t *resp_headers = NULL;
	oss_request_options_t *options = NULL; 
	aos_status_t *s = NULL;
	aos_string_t file;
	aos_list_t resp_body; 

	aos_pool_create(&p, NULL);
	options = oss_request_options_create(p);
	init_sample_request_options(options, is_cname);
	headers = aos_table_make(options->pool, 1); 
	apr_table_set(headers, OSS_CONTENT_TYPE, OSS_TYPE_JPEG);
	aos_str_set(&bucket, BUCKET_NAME);

	//"F:\\downloaded\\Charlie.mp3", "test/101/Charlie.mp3", "audio/mp3"

	std::string remoteobject(TEST_OBJDIR);
	remoteobject.append(paperid_);
	remoteobject.append("/preview.jpg");
	aos_str_set(&object, remoteobject.c_str());
	std::string paperpath(local_paper_path_);
	paperpath.append("\\").append("preview.jpg");;
	aos_str_set(&file, paperpath.c_str());
	aos_list_init(&resp_body);

	s = oss_put_object_from_file(options, &bucket, &object, &file,
		headers, &resp_headers);

	_cprintf("upload %s to %s\n", paperpath.c_str(), remoteobject.c_str());
	//s = oss_do_put_object_from_file(options, &bucket, &object, &file, NULL, NULL, percentage, &resp_headers, &resp_body);

	if (aos_status_is_ok(s)) {
		if (LOGDEBUG) _cprintf("put preview object from file succeeded\n");	 
	}
	else {
		if (LOGDEBUG) _cprintf("put object from file failed, code:%d, error_code : %s, error_msg : %s, request_id : %s\n",
			s->code, s->error_code, s->error_msg, s->req_id); 
		return false;
	}

	aos_pool_destroy(p);
	if (LOGDEBUG) _cprintf("upload done\n");
	return true;
}

bool OSS_Upload::UploadUpup()
{
	aos_pool_create(&pool_, NULL);

	aos_pool_t *p = NULL;
	aos_string_t bucket;
	aos_string_t object;
	int is_cname = 0;
	aos_table_t *headers = NULL;
	aos_table_t *resp_headers = NULL;
	oss_request_options_t *options = NULL;
	aos_status_t *s = NULL;
	aos_string_t file;
	aos_list_t resp_body;

	aos_pool_create(&p, NULL);
	options = oss_request_options_create(p);
	init_sample_request_options(options, is_cname);
	headers = aos_table_make(options->pool, 1);
	apr_table_set(headers, OSS_CONTENT_TYPE, OSS_TYPE_TXT);
	aos_str_set(&bucket, BUCKET_NAME);
	 
	std::string remoteobject(TEST_OBJDIR);
	remoteobject.append(paperid_);
	remoteobject.append("/theme.upup");
	aos_str_set(&object, remoteobject.c_str());
	std::string paperpath(local_paper_path_);
	paperpath.append("\\").append("theme.upup");;
	aos_str_set(&file, paperpath.c_str());
	aos_list_init(&resp_body);

	s = oss_put_object_from_file(options, &bucket, &object, &file,
		headers, &resp_headers);

	_cprintf("upload %s to %s\n", paperpath.c_str(), remoteobject.c_str());
	//s = oss_do_put_object_from_file(options, &bucket, &object, &file, NULL, NULL, percentage, &resp_headers, &resp_body);

	if (aos_status_is_ok(s)) {
		if (LOGDEBUG) _cprintf("put upup object from file succeeded\n");
	}
	else {
		if (LOGDEBUG) _cprintf("put object from file failed, code:%d, error_code : %s, error_msg : %s, request_id : %s\n",
			s->code, s->error_code, s->error_msg, s->req_id);
		return false;
	}

	aos_pool_destroy(p);
	if (LOGDEBUG) _cprintf("upload theme.upup done\n");
	return true;
}


bool OSS_Upload::UploadData()
{
	aos_pool_create(&pool_, NULL);
	aos_pool_t *p = NULL;
	aos_string_t bucket;
	aos_string_t object;
	int is_cname = 0;
	aos_table_t *headers = NULL;
	aos_table_t *resp_headers = NULL;
	oss_request_options_t *options = NULL;
	aos_status_t *s = NULL;
	aos_string_t file;
	aos_list_t resp_body;

	aos_pool_create(&p, NULL);
	options = oss_request_options_create(p);
	init_sample_request_options(options, is_cname);
	headers = aos_table_make(options->pool, 1);
	//apr_table_set(headers, OSS_CONTENT_TYPE, OSS_TYPE_TXT);
	aos_str_set(&bucket, BUCKET_NAME);

	std::string remoteobject(TEST_OBJDIR);
	remoteobject.append(paperid_);
	std::string paperpath(local_paper_path_);

	if (themetype_ == 1) {
		remoteobject.append("/").append(src_);		 
		paperpath.append("\\").append(src_);
		if (src_.find(".mp4") > 0) {
			apr_table_set(headers, OSS_TYPE_UNKNOWN, OSS_TYPE_MP4);
		}
		else if (src_.find(".avi") > 0) {
			apr_table_set(headers, OSS_TYPE_UNKNOWN, OSS_TYPE_AVI);
		}
		else {
			apr_table_set(headers, OSS_TYPE_UNKNOWN, OSS_TYPE_UNKNOWN);
		}
	}
	else {
		remoteobject.append("/").append("paper.zip");
		paperpath.append("\\").append("paper.zip");
		apr_table_set(headers, OSS_TYPE_UNKNOWN, OSS_TYPE_TXT);
	}
	 
	aos_str_set(&object, remoteobject.c_str());
	aos_str_set(&file, paperpath.c_str());
	aos_list_init(&resp_body);

	//s = oss_put_object_from_file(options, &bucket, &object, &file,
	//	headers, &resp_headers);

	_cprintf("upload %s to %s\n", paperpath.c_str(), remoteobject.c_str());
	s = oss_do_put_object_from_file(options, &bucket, &object, &file, NULL, NULL, percentage, &resp_headers, &resp_body);

	if (aos_status_is_ok(s)) {
		if (LOGDEBUG) _cprintf("put daat object from file succeeded\n");
	}
	else {
		if (LOGDEBUG) _cprintf("put object from file failed, code:%d, error_code : %s, error_msg : %s, request_id : %s\n",
			s->code, s->error_code, s->error_msg, s->req_id);
		return false;
	}

	aos_pool_destroy(p);
	if (LOGDEBUG) _cprintf("upload data done\n");
	return true;
}

void OSS_Upload::init_sample_request_options(oss_request_options_t *options, int is_cname) {
	options->config = oss_config_create(options->pool);
	aos_str_set(&options->config->endpoint, OSS_ENDPOINT);
	aos_str_set(&options->config->access_key_id, ACCESS_KEY_ID);
	aos_str_set(&options->config->access_key_secret, ACCESS_KEY_SECRET);
	options->config->is_cname = is_cname;

	options->ctl = aos_http_controller_create(options->pool, 0);

}

void   OSS_Upload::percentage(int64_t consumed_bytes, int64_t total_bytes)
{
	assert(total_bytes >= consumed_bytes);
	if (consumed_bytes > total_bytes)
	if (LOGDEBUG) _cprintf("%%%" APR_INT64_T_FMT "\n", consumed_bytes * 100 / total_bytes);
}

bool OSS_Upload::hasinit_ = false;
